h1.목적 : 폼 위젯의 이벤트 처리 방법을 익힌다.
참조 : 클릭이벤트
h2.클릭 이벤트 구현방법
예> 버튼 이벤트
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
android:onClick 으로 호출되는 메소드의 특징
/** Called when the user touches the button */
public void sendMessage(View view) {
// Do something in response to button click
}
익명클래스 이용
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
액티비티 자체 구현
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
h2.Event Listeners
이벤트 리스터는 View 클래스의 인터페이스로 단일 콜백 메소드를 가지고 있다.
이 메소드는 사용자와의 상호작용에 의한 트리거가 발생되면 Android framework 에 의해 호출된다.
참고 Input Events
h2.실습과제
![]() | ![]() |
![]() | |
![]() | ![]() |
![]() | ![]() |
public static class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (MainActivity)getActivity(), year, month, day);
}
}//end DatePickerDialog
/**
* 데이트피커 선택 후 처리
*/
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
//edt_birthday.setText(new StringBuilder().append(year).append("-")
// .append(month+1).append("-").append(day));
}